package com.easylinkin.linkappapi.safe.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.safe.dao.HiddenConfigMapper;
import com.easylinkin.linkappapi.safe.entity.HiddenConfig;
import com.easylinkin.linkappapi.safe.service.HiddenConfigService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 通知设置 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2022-04-13
 */
@Service
public class HiddenConfigServiceImpl extends ServiceImpl<HiddenConfigMapper, HiddenConfig> implements HiddenConfigService {

  @Autowired
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param hiddenConfig
   */
  @Override
  public void insert(HiddenConfig hiddenConfig) {
    /**
     * 验证重复
     */
    this.checkExist(hiddenConfig);
    //设置基本属性
    this.setBase(hiddenConfig);
    //设置生效时间
    hiddenConfig.setUpperTime(hiddenConfig.getCreateTime());
    this.save(hiddenConfig);
  }

  /**
   * 根据id编辑
   * @param hiddenConfig
   */
  @Override
  public void updateOne(HiddenConfig hiddenConfig) {
    /**
     * 验证重复
     */
    this.checkExist(hiddenConfig);
    //设置基本属性
    this.setBase(hiddenConfig);
    //设置生效时间
    HiddenConfig old = baseMapper.selectById(hiddenConfig.getId());
    if (null != hiddenConfig.getTaskUpper() && !hiddenConfig.getTaskUpper().equals(old.getTaskUpper())){
      hiddenConfig.setUpperTime(hiddenConfig.getModifyTime());
    }
    this.updateById(hiddenConfig);
  }

  @Override
  public HiddenConfig findOne() {
    HiddenConfig hiddenConfig = new HiddenConfig();
    String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
    QueryWrapper<HiddenConfig> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tenant_id_",tenantId);
    List<HiddenConfig> configs = baseMapper.selectList(queryWrapper);
    if (CollectionUtil.isNotEmpty(configs)){
      hiddenConfig = configs.get(0);
    }else {
      hiddenConfig.setRectify(true);
      hiddenConfig.setCheck(true);
      hiddenConfig.setCheckEnd(true);
    }
    return hiddenConfig;
  }

  /**
   * 验证重复
   */
  private void checkExist(HiddenConfig hiddenConfig) {
    QueryWrapper<HiddenConfig> queryWrapper = new QueryWrapper<>();
    //判断重复条件
    //设置判断重复条件
    queryWrapper.eq("tenant_id_",linkappUserContextProducer.getCurrent().getTenantId());
    //编辑的时候存在id
    Optional.ofNullable(hiddenConfig.getId()).ifPresent(id -> queryWrapper.ne("id",hiddenConfig.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该通知设置已存在");
    }
  }

  /**
   * 设置基本属性
   * @param hiddenConfig
   */
  private void setBase(HiddenConfig hiddenConfig) {
    hiddenConfig.setModifyTime(new Date());
    if(null != linkappUserContextProducer.getCurrent()){
      hiddenConfig.setModifyId(linkappUserContextProducer.getCurrent().getId());
    }
    //没有id就是新增,有就是编辑
    if (null == hiddenConfig.getId()){
      if(null != linkappUserContextProducer.getCurrent()){
        hiddenConfig.setCreatorId(linkappUserContextProducer.getCurrent().getId());
      }
      hiddenConfig.setCreateTime(new Date());
      if(StringUtils.isBlank(hiddenConfig.getTenantId())){
        hiddenConfig.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
      }
    }
  }
}
